package com.szcinda.service.dto.role;

import com.szcinda.repository.Role;
import com.szcinda.repository.RoleType;
import com.szcinda.repository.User;
import com.szcinda.repository.UserRepository;
import com.szcinda.service.SnowFlakeFactory;
import com.szcinda.service.dto.log.LogCreateDto;
import com.szcinda.service.dto.log.LogService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    private final RoleRepository roleRepository;
    private final SnowFlakeFactory snowFlakeFactory;
    private final UserRepository userRepository;
    private final LogService logService;

    public RoleServiceImpl(RoleRepository roleRepository, UserRepository userRepository, LogService logService) {
        this.roleRepository = roleRepository;
        this.userRepository = userRepository;
        this.logService = logService;
        this.snowFlakeFactory = SnowFlakeFactory.getInstance();
    }

    @Override
    public void create(RoleCreateDto createDto) {
        Assert.isTrue(!createDto.getName().equals(RoleType.ADMIN) && !createDto.getName().equals(RoleType.ADMIN2) && !createDto.getName().equals(RoleType.SUPER_ADMIN), "名称不合法");
        Role record = roleRepository.findByName(createDto.getName());
        Assert.isTrue(record == null, "存在名称为[" + createDto.getName() + "]的角色");
        Role role = new Role();
        BeanUtils.copyProperties(createDto, role);
        role.setId(snowFlakeFactory.nextId("RO"));
        roleRepository.save(role);
        User user = userRepository.findById(createDto.getOpId());
        LogCreateDto logCreateDto = new LogCreateDto(user.getId(), user.getNickName(), "新增角色", role.getName());
        logService.create(logCreateDto);
    }

    @Override
    public void update(RoleUpdateDto updateDto) {
        Role role = roleRepository.findById(updateDto.getId());
        role.setRemark(updateDto.getRemark());
        role.setPermissions(updateDto.getPermissions());
        role.setCanCheckData(updateDto.getCanCheckData());
        role.setNotice(updateDto.isNotice());
        role.setEqualAdmin(updateDto.isEqualAdmin());
        roleRepository.save(role);
        User user = userRepository.findById(updateDto.getOpId());
        LogCreateDto logCreateDto = new LogCreateDto(user.getId(), user.getNickName(), "修改角色", role.getName());
        logService.create(logCreateDto);
    }

    @Override
    public List<Role> findAll() {
        return roleRepository.findAll();
    }

    @Override
    public List<Role> findAllWithoutSuperAdmin() {
        return roleRepository.findAll().stream().filter(role -> !role.getName().equals(RoleType.SUPER_ADMIN)).collect(Collectors.toList());
    }

    @Override
    public void delete(String id, String userId) {
        Role role = roleRepository.findById(id);
        roleRepository.delete(role);
        User user = userRepository.findById(userId);
        LogCreateDto logCreateDto = new LogCreateDto(user.getId(), user.getNickName(), "删除角色", role.getName());
        logService.create(logCreateDto);
    }
}
